package com.atguigu.springcloud.filter;

import ch.qos.logback.core.net.SocketConnector;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.time.Instant;

/**
 * @auther zzyy
 * @create 2020-02-07 11:53
 */
@Component
public class CustomerGateWayFilter implements GatewayFilter,Ordered
{


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
    {
        exchange.getAttributes().put("StartTime",Instant.now().toEpochMilli());

        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            long startTime = exchange.getAttribute("StartTime");
            long endTime = (Instant.now().toEpochMilli() - startTime);
            System.out.println(exchange.getRequest().getURI().getRawPath()+"\t"+endTime+"\t ms");
        }));
    }

    @Override
    public int getOrder()
    {
        return 0;
    }
}
